const { ManagerTempBlob } = require('./managertempblob')
const {
    TEMP_FILENAME,
    MARK_FLAGS,
    saveTempFile,
    deleteTempFiles,
    timeConvart
} = require('./share')
const {    
    merge_v,
    merge_a,
    merge_v_a,
    merge_a_a,
    merge_v_a_a
} = require('./ffmpegsynthesis')
const path = require('path')

const managerTempBlob = new ManagerTempBlob()
function saveTempBlob(data, mark) {
    managerTempBlob.addTempBlob(data, mark)
}

function saveFile(tempP, savePath) {
    const tempPath = path.resolve(tempP, 'hhlptemporary')
    // 临时任务
    const tempTasks = []
    managerTempBlob.tempBlobs.forEach((value, key) => {
        tempTasks.push(saveTempFile(value, TEMP_FILENAME[key], tempPath))
    })
    // 保存临时文件
    return Promise.all(tempTasks).then(() => {
        // 获取当前时间
        const date = new Date()
        // SV SA
        if (managerTempBlob.checkComb(MARK_FLAGS['SV'] | MARK_FLAGS['SA'])) {
            return merge_v_a(path.resolve(tempPath, TEMP_FILENAME['SV']), path.resolve(tempPath, TEMP_FILENAME['SA']), path.resolve(savePath, timeConvart(date) + 's_video_s_audio.mp4'))
        }
        // SV cv
        if (managerTempBlob.mark === 4) {
            console.log('画中画')
            return
        }

        // SV MA
        if (managerTempBlob.checkComb(MARK_FLAGS['SV'] | MARK_FLAGS['MA'])) {
            return merge_v_a(path.resolve(tempPath, TEMP_FILENAME['SV']), path.resolve(tempPath, TEMP_FILENAME['MA']), path.resolve(savePath, timeConvart(date) + 's_video_m_audio.mp4'))
        }

        // SA MA
        if (managerTempBlob.checkComb(MARK_FLAGS['SA'] | MARK_FLAGS['MA'])) {
            return merge_a_a(path.resolve(tempPath, TEMP_FILENAME['SA']), path.resolve(tempPath, TEMP_FILENAME['MA']), path.resolve(savePath, timeConvart(date) + 's_m_audio.mp3'))
        }
        // SV SA MA
        if (managerTempBlob.checkComb(MARK_FLAGS['SV'] | MARK_FLAGS['SA'] | MARK_FLAGS['MA'])) {
            return merge_v_a_a(path.resolve(tempPath, TEMP_FILENAME['SV']), path.resolve(tempPath, TEMP_FILENAME['SA']), path.resolve(tempPath, TEMP_FILENAME['MA']), path.resolve(savePath, timeConvart(date) + 's_video_s_m_audio.mp4'))
        }
        // SV cv SA
        if (managerTempBlob.mark === 13) {
            console.log('画中画')
            return
        }
        // SV cv MA
        if (managerTempBlob.mark === 8) {
            console.log('画中画')
            return
        }
        // SV SA cv MA
        if (managerTempBlob.mark === 9) {
            console.log('画中画，音频混合')
        }
        // SV
        if (managerTempBlob.checkComb(MARK_FLAGS['SV'])) {
            return merge_v(path.resolve(tempPath, TEMP_FILENAME['SV']), path.resolve(savePath, timeConvart(date) + 's_video.mp4'))
        }

        // cv
        if (managerTempBlob.checkComb(MARK_FLAGS['CV'])) {
            return merge_v(path.resolve(tempPath, TEMP_FILENAME['CV']), path.resolve(savePath, timeConvart(date) + 's_video.mp4'))
        }

        // SA
        if (managerTempBlob.checkComb(MARK_FLAGS['SA'])) {
            return merge_a(path.resolve(tempPath, TEMP_FILENAME['SA']), path.resolve(savePath, timeConvart(date) + 's_audio.mp4'))
        }

        // MA
        if (managerTempBlob.checkComb(MARK_FLAGS['MA'])) {
            return merge_a(path.resolve(tempPath, TEMP_FILENAME['CV']), path.resolve(savePath, timeConvart(date) + 'm_audio.mp4'))
        }
    }).finally(() => {
        managerTempBlob.clear()
        // 删除temporary文件夹下所有文件
        deleteTempFiles(tempPath)
    })
}

module.exports = {
    saveTempBlob,
    saveFile
}
